package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.constant.AuthConstant;
import com.atguigu.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class OrderWebIntereptor implements HandlerInterceptor {
    public static  ThreadLocal<MemberVo> loginUser = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String[] uris = new String[]{
                "/order/order/orderStatus/**",
                "order/order/member/orders/**"
        };
        String uri = request.getRequestURI();
        //放行部分服务器内部访问
//        /order/order/orderStatus/{orderSn}
        boolean match = new AntPathMatcher().match("/order/order/orderStatus/**", uri);
        if(match){
            return true;
        }
        //判断用户是否登录
        MemberVo member = (MemberVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);
        if(member!=null){
            //已登录,放行
            loginUser.set(member);
            return true;
        }
        //未登录，重定向到登录页
        request.getSession().setAttribute("msg","您还未登录，请登录");
        response.sendRedirect("http://auth.gulimall.com/login.html");
        return false;
    }

}
